import {
	BASEURL,
	TITLE
} from '@/env.js'
// import store from '@/common/store'
export const $request = (url, data, config, method) => {
	if (config?.config?.loading) { //是否显示加载
		uni.showLoading({
			title: '数据加载中...'
		})
	}
	const promise = new Promise((resolve, reject) => {
		let sktoken = uni.getStorageSync("token")
		let that = this
		uni.request({
			url: BASEURL + url,
			data,
			method,
			header: {
				"content-type": 'application/json',
				'token': sktoken || '',
			},
			withCredentials: true,
			success: (res) => {
				if (config?.config?.loading)
					uni.hideLoading()
				if (res.code === 1) {
					if (config?.config?.toast === true) {
						uni.$u.toast(res.msg)
					}
				}
				resolve(res.data)
			},
			complete: (response) => {
				console.log(response, 'responseresponse');
				if (response.data.code === 401) {
					uni.clearStorageSync();
					// that.$refs.setuser.open()
					// var pages=getCurrentPages()
					// uni.$u.toast(response.data.msg)
					// setTimeout(function(){
					// 	uni.navigateTo({
					// 		url:'/pages/login/login'
					// 	})
					// },1500)
					// uni.showToast({
					//   	title:"",
					//   	icon: 'none',
					//   	duration:1500
					// })
				} else if (response.code === 500) {
					uni.$u.toast('服务器错误')
				}
			}
		})
	})
	return promise
}